clear all
format short
FALSE = 0;
TRUE = 1;
%==========================================================================
%constants
P = 32;
TRAINING_P = 32;%amount of training patterns
LAYERS = 3;
INPUT_LENGHT = 5;
OUTPUT_LENGHT = 4;
load inputMatrix.mat
load outputMatrix.mat

%==========================================================================
%initialization
%rand('seed', 7);
%get P training patterns
[trainingPatterns trainingResults] = selectTrainingPatterns(TRAINING_P, inputMatrix, outputMatrix);
stop = FALSE;
net = constructNet(INPUT_LENGHT, OUTPUT_LENGHT, LAYERS);

%==========================================================================
%train the neuronal net
epoc = 1;
passed = 0;
while((stop == FALSE))
    %present the patterns in a different order each time
    [trainingPatterns trainingResults] = shufflePatterns(TRAINING_P, trainingPatterns, trainingResults);
    LEARNING_RATE=calcLearningRate(passed, TRAINING_P);
    net = trainNet(net, trainingPatterns, trainingResults, LEARNING_RATE);
    [passed ECM totalError] = hasLearned(net, trainingPatterns, trainingResults);
    %%%%stop = passed == size(trainingPatterns,1);
    if (ECM < 0.04 || passed == size(trainingPatterns,1))
       stop = 1;
    end
    learning_plot(epoc) = passed;
    emc_error(epoc) = ECM;
    total_error(epoc) = totalError;
    epoc = epoc + 1;
    if(mod(epoc,50)==0)
        subplot(2,2,1:2); plot(emc_error);
        subplot(2,2,3:4); plot(learning_plot);
        drawnow;
    end
end



pasaron = passed
error_cuadratico_medio = ECM
epocas = epoc
%hold on
%plot(learning_plot);
%hold on
%plot(emc_error);
%plot(total_error, 'r');
subplot(2,2,1:2); plot(emc_error);
subplot(2,2,3:4); plot(learning_plot);
        
%==========================================================================
%testing all the patterns
passed_test=0;
for k=1:P
    passed_test = passed_test + checkEquals(net, inputMatrix(k,:), outputMatrix(k,:));
end

percentPassed = (100.0 * passed_test)/P
